package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.DocCheckList;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.MemberConstant;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class AdminMemberDocCheckListBean extends AbstractBean {

	public static final String BEAN_NAME="adminMemberDocCheckListBean";
	public static final String NAV_LIST="adminMemberDocList";
	public static final String NAV_CREATE="adminMemberDocCreate";
	public static final String NAV_EDIT="adminMemberDocEdit";
	
	private MemberService service;
	private DocCheckList checkList;
	private List<DocCheckList> docList;
	private List<SelectItem> manSelectList;
	private List<SelectItem> memberSelectList;
	
	public DocCheckList getCheckList() {
		return checkList;
	}

	public void setCheckList(DocCheckList checkList) {
		this.checkList = checkList;
	}

	public void setService(MemberService service) {
		this.service = service;
	}

	/*------------------------------------------------------------*/
	public String navListAction(){
		return NAV_LIST;
	}
	
	public List<DocCheckList> getDocList() {
		try{
			docList = service.getDocCheckListList();
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return docList;
	}
	
	public String navCreateAction(){
		checkList = new DocCheckList();
		return NAV_CREATE;
	}
	
	public String createAction(){
		try{
			service.createDocCheckList(checkList, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_CREATE;
	}
	
	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("docId");
		String id = (String)param.getValue();
		for(DocCheckList d:docList){
			if(d.getDocCheckListId().equalsIgnoreCase(id)){
				checkList = d;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT;
	}
	
	public String editAction(){
		try{
			service.editDocCheckList(checkList, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_EDIT;
	}
	
	public String deleteAction(){
		try{
			service.deleteDocCheckList(docList,getThisUser());
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_LIST;
	}

	public List<SelectItem> getManSelectList() {
		
		manSelectList = new ArrayList<SelectItem>();
		SelectItem item = new SelectItem(MemberConstant.MEMBER_DOC_MANDATORY,
				SpringUtils.getMessage(MemberConstant.MEMBER_DOC_MANDATORY, new Object[]{}));
		SelectItem item1 = new SelectItem(MemberConstant.MEMBER_DOC_NOT_MANDATORY,
				SpringUtils.getMessage(MemberConstant.MEMBER_DOC_NOT_MANDATORY, new Object[]{}));
		
		manSelectList.add(item);
		manSelectList.add(item1);
		return manSelectList;
	}

	public List<SelectItem> getMemberSelectList() {
		
		memberSelectList = new ArrayList<SelectItem>();
		SelectItem item = new SelectItem(CommonConstant.MEMBER_TYPE_COMP,
				SpringUtils.getMessage(CommonConstant.MEMBER_TYPE_COMP, new Object[]{}));
		SelectItem item1 = new SelectItem(CommonConstant.MEMBER_TYPE_IND,
				SpringUtils.getMessage(CommonConstant.MEMBER_TYPE_IND, new Object[]{}));
		
		memberSelectList.add(item);
		memberSelectList.add(item1);
		
		return memberSelectList;
	}
	
	
}
